package dtu.fmes.loon.utils;

import dtu.fmes.loon.model.Vector;
import dtu.fmes.loon.model.environment.ImportWind;
import dtu.fmes.loon.model.environment.Wind;

public class VectorUtils {

	private VectorUtils() {

	}

	public static Double getLength(Vector v) {
		return Math.sqrt(v.getX() * v.getX() + v.getY() * v.getY());
	}

	public static Double getAngleBetweenVectors(Vector a, Vector b) {
		Double aLength = getLength(a);
		Vector aNormalized = new Vector(a.getX() / aLength, a.getY() / aLength);
		Double bLength = getLength(b);
		Vector bNormalized = new Vector(b.getX() / bLength, b.getY() / bLength);

		Double dotProduct = aNormalized.getX() * bNormalized.getX() + aNormalized.getY() * bNormalized.getY();

		return Math.toDegrees(Math.acos(dotProduct));
	}

	public static Vector moveToOrigo(Vector start, Vector end) {
		Double x = end.getX() - start.getX();
		Double y = end.getY() - start.getY();

		return new Vector(x, y);
	}

	public static Wind[][][] toWindArray(ImportWind[][][] importWinds) {
		int dimens1 = importWinds.length;
		int dimens2 = importWinds[0].length;
		int dimens3 = importWinds[0][0].length;
		Wind[][][] winds = new Wind[dimens1][dimens2][dimens3];

		for (int i = 0; i < dimens1; i++) {
			for (int j = 0; j < dimens2; j++) {
				for (int k = 0; k < dimens3; k++) {
					winds[i][j][k] = extractWind(importWinds[i][j][k]);
				}
			}
		}

		return winds;
	}

	public static Wind extractWind(ImportWind iWind) {
		double x = Math.sin(Math.toRadians(iWind.getAngle())) * iWind.getMagnitude();
		double y = Math.cos(Math.toRadians(iWind.getAngle())) * iWind.getMagnitude();

		Vector vector = new Vector(x, y);

		return new Wind(iWind.getHeight(), vector);
	}
}
